package logic

import (
	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"
	"beike/models"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type ResAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResAddLogic {
	return &ResAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResAddLogic) ResAdd(in *houserpc.ResAddRequest) (*houserpc.ResAddResponse, error) {
	// todo: add your logic here and delete this line

	var res models.Reservation
	models.DB.Debug().Where("house_id = ?", in.HouseId).Where("res_time = ?", in.ResTime).Find(&res)

	if res.ID == 0 {
		res = models.Reservation{
			UserId:  in.UserId,
			HouseId: in.HouseId,
			ResTime: in.ResTime,
			Status:  "预约中",
		}
		models.DB.Debug().Create(&res)
	}

	return &houserpc.ResAddResponse{
		ResId: int64(res.ID),
	}, nil
}
